package com.paic.mhis.hcpms.agreement.healthcareprovider.web;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.hcpms.agreement.healthcareprovider.biz.service.AccountService;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.Constants;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.HttpProcessUtil;
import com.paic.mhis.hcpms.agreement.healthcareprovider.util.ValidatorUtil;
import com.paic.pafa.appclient.annotation.BizServicesProxy;
import com.paic.pafa.web.BaseRest;

@Controller
public class AccountController extends BaseRest {
	@BizServicesProxy
	private AccountService accountService;

	public void setBreachService(AccountService accountService) {
		this.accountService = accountService;
	}

	/**
	 * 帐户信息新增
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/account/create", method = RequestMethod.POST)
	public Map<String, Object> addAccount(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJson(request, transferJson);
			result = this.accountService.addAccount(transferJson);

		} catch (Exception e) {

			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 账户信息修改
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/account/update", method = RequestMethod.POST)
	public Map<String, Object> updateAccount(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJsonUpdate(request, transferJson);
			result = this.accountService.updateAccount(transferJson);
		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 账户信息删除
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/account/delete", method = RequestMethod.POST)
	public Map<String, Object> deleteAccount(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String str = HttpProcessUtil.readRequestBody(request);
			JSONObject transferJson = JSON.parseObject(str);
			// assembleTransferJsonUpdate(request, transferJson);
			result = this.accountService.deleteAccount(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	/**
	 * 账户信息列表查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/account/page")
	public Map<String, Object> queryAccountList(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {

			String providerid = request.getParameter("providerid");
			String bankname = request.getParameter("bankname");
			String holder = request.getParameter("holder");
			String accountno = request.getParameter("accountno");
			String page = request.getParameter("page");
			String pageSize = request.getParameter("pageSize");
			if (!isEmpty(page) || !isEmpty(pageSize)) {
				if (!ValidatorUtil.isPlusInteger(page)
						|| !ValidatorUtil.isPlusInteger(pageSize)) {
					result = new HashMap<String, Object>();
					HttpProcessUtil.generateExceptionResult(result,
							Constants.STATE_KEY,
							Constants.FAILED_VALIDATE_STATE_VALUE,
							Constants.MSG_KEY,
							Constants.PAGE_VALIDATE_FAILED_MSG);
					return result;
				}
			}
			JSONObject transferJson = new JSONObject();
			transferJson.put("page", page);
			transferJson.put("pageSize", pageSize);
			transferJson.put("providerid", providerid);
			transferJson.put("bankname", bankname);
			transferJson.put("accountno", accountno);
			transferJson.put("holder", holder);
			result = this.accountService.queryAccountList(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

	private boolean isEmpty(String param) {
		return param == null || param.equals("");
	}

	/**
	 * 账户信息详情查询
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/hcp/hcp/account/show")
	public Map<String, Object> queryAccountDetail(HttpServletRequest request) {
		Map<String, Object> result = null;
		try {
			String id = request.getParameter("id");
			JSONObject transferJson = new JSONObject();
			transferJson.put("id", id);
			result = accountService.queryAccountDetail(transferJson);

		} catch (Exception e) {
			result = HttpProcessUtil.processException(e);
		}
		return result;
	}

}
